package service

import (
	"context"
	"errors"
	__ "order_srv/basic/orderproto"
	"order_srv/handler/dao"
)

// DispatchConfigCreateOrUpdate 创建或更新派单配置
func (s *Server) DispatchConfigCreateOrUpdate(_ context.Context, in *__.DispatchConfigCreateOrUpdateReq) (*__.DispatchConfigCreateOrUpdateResp, error) {
	update, err := dao.DispatchConfigCreateOrUpdate(in)
	if err != nil {
		return nil, errors.New(err.Error())
	}
	return &__.DispatchConfigCreateOrUpdateResp{
		Id: int64(update.Id),
	}, nil
}

// DispatchConfigGet 获取派单配置
func (s *Server) DispatchConfigGet(_ context.Context, in *__.DispatchConfigGetReq) (*__.DispatchConfigGetResp, error) {
	p := dao.DispatchConfigGet(in)
	return &__.DispatchConfigGetResp{
		CityId:              p.CityId,
		Strategy:            p.Strategy,
		MaxDistanceKM:       float32(p.MaxDistanceKM),
		ScoreWeightDistance: float32(p.ScoreWeightDistance),
		ScoreWeightRating:   float32(p.ScoreWeightRating),
		ScoreWeightAccept:   float32(p.ScoreWeightAccept),
		DayStartHour:        int64(p.DayStartHour),
		NightStartHour:      int64(p.NightStartHour),
		TimeWindowMinutes:   int64(p.TimeWindowMinutes),
		GrayGroups:          p.GrayGroups,
		Version:             p.Version,
	}, nil
}

// DispatchConfigDelete 删除派单配置
func (s *Server) DispatchConfigDelete(_ context.Context, in *__.DispatchConfigDeleteReq) (*__.DispatchConfigDeleteResp, error) {
	_, err := dao.DispatchConfigDelete(in)
	if err != nil {
		return nil, errors.New(err.Error())
	}
	return &__.DispatchConfigDeleteResp{}, nil
}

// DispatchGrayPublish 发布派单灰度配置
func (s *Server) DispatchGrayPublish(_ context.Context, in *__.DispatchGrayPublishReq) (*__.DispatchGrayPublishResp, error) {
	dispatchPublish, err := dao.DispatchGrayPublish(in)
	if err != nil {
		return nil, errors.New(err.Error())
	}
	return &__.DispatchGrayPublishResp{
		Id: int64(dispatchPublish.CityId),
	}, nil
}

// DispatchGrayRollback 派单配置灰度回滚
func (s *Server) DispatchGrayRollback(_ context.Context, in *__.DispatchGrayRollbackReq) (*__.DispatchGrayRollbackResp, error) {
	dispatchRollback, err := dao.DispatchGrayRollback(in)
	if err != nil {
		return nil, errors.New(err.Error())
	}
	return &__.DispatchGrayRollbackResp{
		Id: int64(dispatchRollback),
	}, nil
}
func (s *Server) DispatchAuditQuery(_ context.Context, in *__.DispatchAuditQueryReq) (*__.DispatchAuditQueryResp, error) {
	list, err := dao.DispatchAuditQuery(in)
	if err != nil {
		return nil, errors.New(err.Error())
	}
	return &__.DispatchAuditQueryResp{
		List: list,
	}, nil
}
